<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/18
 * Time: 14:38
 */

namespace Home\Controller;
use Home\Model\UserModel;
use Think\Controller;

class RegisterController extends BaseController {


    /*
     * 提交注册资料完成信息的写入
     * @param string $mobile //用户注册的手机号，同时注册短信下发到该手机号
     * @param array $data //用户的注册信息
     * @return json $result //注册的状态返回信息
     */
    public function register_userinfo()
    {
        $model = M('userinfo');
        $mobile = I('post.mobile');//手机号，通过手机号查询用户的id
        $user_id = $this->select_user_id($mobile);//通过手机号码查询用户id
        $condition['user_id'] = $user_id;
        $nickname = I('post.nickname');
        $sex = I('post.sex');//传id，为1则是男，为2则是女
        $hope_marry_time = I('post.hope_marry_time');//marry表id
        $annual_income = I('post.annual_income');//annual_income表id
        $region_name = I('post.city');
        $city = $this->select_city_id($region_name);//通过最下级地区名查询在数据表中的id
        $chengshi = $this->select_chengshi_id($region_name);//查询对应城市的id
        $data = array(
            'user_id'=>$user_id,
            'hope_marry_time'=>$hope_marry_time,
            'annual_income'=>$annual_income,
            'city'=>$chengshi,
            'chensghi'=>$city,
        );
        //进行非空验证，如果昵称为空就不进行修改
        if($nickname!=""){
        	$data['nickname']=$nickname;
        }
        //进行非空验证，如果性别为空就不进行修改
        if($sex!=""){
        	$data['sex']=$sex;
        }
        $res = $model->where($condition)->save($data);
        if(!empty($res))
        {
            $result = array(
                'msg'=>'success',
                'statu'=>1
            );
            $this->update_type($user_id);
        }else{
            $result = array(
                'msg'=>'failed',
                'statu'=>2
            );
        }
        $result['nickname']=$nickname;
        R('sms/send_sms', array($mobile,4));//调用短信下发接口提醒进行身份认证
        echo json_encode($result);
    }

    /*
     * 更新用户的注册状态，将状态变更为5：提交资料为注册状态
     * @param int $id //用户的id
     */
    public function update_type($id)
    {
        $model = M('user');
        $condition['id'] = $id;
        $type['type'] = 5;
        $model->where($condition)->save($type);
    }

    public function select_chengshi_id($city)
    {
        $model = M('region');
        $array =explode(',',$city);
        $condition['region_name'] = $array[0];
        $data = $model->where($condition)->select();
        $city_id = $data[0]['id'];
        return $city_id;
    }

    /*
     * 通过城市查询对应的区的id
     * @param string $city //传入的省份，城市，用explode分割成数组再取出城市
     * @param int $county_id //查出对应的省份id
     */
    public function select_city_id($city)
    {
        $model = M('region');
        $array =explode(',',$city);
        $condition['region_name'] = $array[0];
        $data = $model->where($condition)->select();
        $city_id = $data[0]['id'];
        $children['parent_id'] = $city_id;
        $children['region_name'] = $array[1];
        $county_id = $model->where($children)->select();;
        return $county_id[0]['id'];
    }

    /*
     * 通过手机号码查询用户的id
     * @param string $mobile //用户的手机号码
     * @return int $id //返回该手机号码对应的id
     */
    public function select_user_id($mobile)
    {
        $model = M('user');
        $condition['user'] = $mobile;
        $data = $model->where($condition)->select();
        $id = $data[0]['id'];
        return $id;
    }

    /*
     * 获取年收入信息
     * @return json $data 返回年收入信息表的数据
     */
    public function get_income()
    {
        $model = M('income');
        $condition['statu'] = 1;
        $data = $model->where($condition)->select();
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    /*
     * 获取结婚信息表的数据
     * @return json //返回期望结婚时间列表
     */
    public function get_marryinfo()
    {
        $model = M('marry');
        $condition['statu'] = 1;
        $data = $model->where($condition)->select();
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    /*
     * 上传身份证图片接口
     * @param string $mobile //手机号码
     * @param int $type //上传照片的类型1为手持照，2为身份证正面照
     * @param array $data //写入数据库的数据
     * @return json 上传的状态信息
     */
    public function upload_pic()
    {
        $model = M('images');
        $mobile = I('post.mobile');
        $user_id = $this->select_user_id($mobile);
        $result['mobile'] = $mobile;
        $type = I('post.type');//如果是1则是身份证正面照，11身份证反面照
        //如果有原有的照片进行删除覆盖
        $this->delete_pic($user_id,$type);
        if(!empty($_FILES['pic'])) {
            $setting=C('UPLOAD_SITEIMG_QINIU');
            $Upload = new \Think\Upload($setting);
            $info = $Upload->upload($_FILES);
            $url = $info['pic']['url'];
            $image = $this->imageinfo($url);
            $imageinfo = array(
                'user_id'=>$user_id,
                'pic_url'=>$url,
                'width'=>$image['width'],
                'height'=>$image['height'],
                'type'=>$type,
                'up_time'=>date('Y-m-d H:i:s',time()),
            );
            $res = $model->data($imageinfo)->add();
            if(!empty($res))
            {
                $data = array(
                    'msg'=>'上传成功！',
                    'statu'=>1,
                    'url'=>$url
                );
            }else{
                $data = array(
                    'msg'=>'上传失败！',
                    'statu'=>2
                );
            }
        }

        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    /*
     * 删除原有的照片
     * @param int $user_id //用户的id
     * @param int $type //图片的状态类型
     * @return 没有返回值
     */
    public function delete_pic($user_id,$type)
    {
        $model = M('images');
        $condition['user_id'] = $user_id;
        $condition['type'] = $type;
        $model->where($condition)->delete();

    }

    /*
     * 改变用户的注册状态
     * @param int $id //用户的唯一id
     * @param int $type //照片的状态类型
     * @return json //删除状态信息
     */
    public function change_type()
    {
        $model = M('user');
        $id = I('post.id');
        $type = I('post.type');
        $result['type'] = $type;
        $condition['id'] = $id;
        $res = $model->where($condition)->save($result);
        if(!empty($res))
        {
            $data =array(
                'msg'=>'change type is success',
                'statu'=>1,
            );
        }else{
            $data = array(
                'msg'=>'change type is failed',
                'statu'=>2,
            );
        }
        echo json_encode($data);
    }

    //检查认证码是否正确
    public function check_auth_code()
    {
        $model = new UserModel();
        $user_id = I('post.user_id');
        $code = I('post.code');
//        $code = 'wxfbrv';
        $res = $model->check_auth_code($code);
        if(empty($res))
        {
            $result = array(
                'msg'=>'认证码不正确！',
                'statu'=>1
            );
        }else{
            $update = $model->update_auth_code($code,$user_id);
            if(empty($update))
            {
                $result = array(
                    'msg'=>'认证码使用失败',
                    'statu'=>2,
                );
            }else{
                $model->change_type($user_id,4);
                $result = array(
                    'msg'=>'认证码使用成功！',
                    'statu'=>3
                );
            }
        }
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }


}